/*
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx;

import wtkx.ui.Cursor;
import wtkx.ui.Keyboard;
import wtkx.ui.Mouse;

import java.awt.Color;
import java.awt.Font;
import java.net.URL;

/**
 *
 * @author gbrown
 * @author jdp
 */
public class Button
    extends Component
{

    public Button() {
        super();
        this.validateTopDown = false;   //(see component)
        this.focusableComponent = true;
        this.setCursor(Cursor.HAND);
        this.transfersFocus = true;
    }


    public void layout(){
        super.layout();
    }
    public Image getIcon() {
        if (this.hasChildren()){
            for (Component child : this){
                if (child instanceof Image){
                    Image image = (Image)child;
                    if (image.isDisplayable() && image.isVisible())
                        return image;
                }
            }
        }
        return null;
    }
    public Image setIcon(Image icon) {
        if (null != icon){

            if (this.hasChildren()){
                Image image = null, re = null;
                for (Component child : this){
                    if (child instanceof Image){
                        image = (Image)child;
                        if (icon.equals(image)){
                            re = image;
                            synchronized(image){
                                image.setDisplayBlock();
                                image.setVisible(true);
                            }
                        }
                        else {
                            synchronized(image){
                                image.setDisplayNone();
                                image.setVisible(false);
                            }
                        }
                    }
                    else {
                        synchronized(child){
                            child.setDisplayNone();
                            child.setVisible(false);
                        }
                    }
                }
                if (null != re){
                    this.invalidate();
                    return re;
                }
            }
            icon.setPadding(this.getPadding());
            this.add(icon);
        }
        return icon;
    }
    public void setIcon(URL url) {
        if (null != url){
            Image icon = new Image(url);
            this.setIcon(icon);
        }
    }
    public void setIcon(String iconName) {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        this.setIcon(classLoader.getResource(iconName));
    }
    public String getLabel(){
        if (this.hasChildren()){
            for (Component child : this){
                if (child instanceof Label){
                    Label label = (Label)child;
                    if (label.isDisplayable() && label.isVisible())
                        return label.getText();
                }
            }
        }
        return null;
    }
    public void setLabel(String text){

        if (this.hasChildren()){
            boolean found = false;
            for (Component child : this){
                if (child instanceof Label){
                    Label label = (Label)child;
                    if (label.isText(text)){
                        found = true;
                        label.setDisplayBlock();
                        label.setVisible(true);
                    }
                    else {
                        label.setDisplayNone();
                        label.setVisible(false);
                    }

                }
                else {
                    child.setDisplayNone();
                    child.setVisible(false);
                }
            }
            if (found){
                this.invalidate();
                return;
            }
        }

        Label label = new Label(text);
        label.setPadding(this.getPadding());
        this.add(label);

    }
    public boolean isSubmit(){
        return this.submit;
    }
    public void setSubmit(boolean b){
        this.submit = b;
    }
    public boolean keyPressed(int keyCode, Keyboard.KeyLocation keyLocation) {
        if (Keyboard.KeyCode.ENTER == keyCode)
            return this.onclick(this,Mouse.Button.LEFT,0,0);
        else
            return super.keyPressed(keyCode,keyLocation);
    }
    public void endElement(){
        for (Component child : this){
            child.setPadding(this.getPadding());
        }
    }
}
